//
//  TDAddTryDriveQuanC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/21.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDAddTryDriveQuanC.h"
#import "TDYuDingDetailC.h"
#import "NetWorkRequest.h"
@interface TDAddTryDriveQuanC ()
@property (weak, nonatomic) IBOutlet UITextField *tf_couponno;
@property (weak, nonatomic) IBOutlet UITextField *tf_verifycode;
@property (weak, nonatomic) IBOutlet UIButton *btn_bind;

@end

@implementation TDAddTryDriveQuanC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
//    [self loadData];
}

- (IBAction)navBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    _btn_bind.layer.borderColor = [UIColor colorWithHex:0xbf0021].CGColor;
    _btn_bind.layer.borderWidth = 1;
    _btn_bind.layer.cornerRadius = 5;
    _btn_bind.layer.masksToBounds = YES;
    //添加tableView
//    [self setupTableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_tf_couponno becomeFirstResponder];
}

// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    
}

- (IBAction)verifycodeBtnClick:(UIButton *)sender {
    [self loadNetData];
}


// MARK: - 获取网络数据-----
- (void)loadNetData {
    if ([HelpObject isBlankString:_tf_couponno.text]) {
        self.noticeText = @"请输入试驾券号码";
        return;
    }
    if ([HelpObject isBlankString:_tf_verifycode.text]) {
        self.noticeText = @"请输入试驾券验证码";
        return;
    }
    NSDictionary *parDic = @{
                             @"couponno":_tf_couponno.text,
                             @"verifycode":_tf_verifycode.text,
                             };
    __weak typeof(self) weakSelf = self;
    [self progressHUDShowWithText:nil];
    [NetWorkRequest postNetRequestData:CouponBind parmeters:parDic response:^(NSDictionary *response) {
        if (response) {
            [weakSelf progressHUDHideImmediately];
            
            if (weakSelf.isFromCal) {
                //跳回到上上层级---因为上个页面不会刷新---
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:[TDYuDingDetailC class]]) {
                        [weakSelf.navigationController popToViewController:controller animated:YES];
                    }
                }
                return;
            }
            
            if (weakSelf.addTryDriveQuanSuccessBlock) {
                weakSelf.addTryDriveQuanSuccessBlock();
            }
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else {
            [weakSelf progressHUDHideImmediately];
        }
    } netError:^(NSError *error) {
        [weakSelf progressHUDShowWithCompleteText:nil isSucceed:NO];
    }];
}
@end
